<?php

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Mvc\View;
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * Setting up the view component
 */
$di->set('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->path->views);

    $view->registerEngines(array(
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php',
    ));
    return $view;
}, true);

$di['db'] = function () use ($config) {
    $params            = $config->db->mysql->toArray();
    $params['charset'] = 'UTF8';
    return new \Phalcon\Db\Adapter\Pdo\Mysql($params);
};

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});

$di->set('cookies', function () {
    $cookies = new Phalcon\Http\Response\Cookies();
    // $cookies->useEncryption(false);
    return $cookies;
});

$di->set('crypt', function () {
    $crypt = new Phalcon\Crypt();
    $crypt->setKey('#1dj8$=dp?.ak//j'); //Use your own key!
    return $crypt;
});

/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

/** 配置信息注入 */
$di->set('config', function () use ($config) {
    return $config;
}, true);

/** 调度器注入（注册调度事件响应处理） */
$di->set('dispatcher', function () use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    // $authenticate  = new \Authenticate($di);
    // $eventsManager->attach('dispatch', $authenticate);
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
}, true);
